//
//  MMPopViewController.swift
//  XLProject
//
//  Created by xinglian on 2022/1/7.
//

import UIKit
@objc protocol MMPopViewControllerDelegate : UIPopoverPresentationControllerDelegate
{
    /// 选中的cell
    @objc  func select(_ index : Int , _ title : String)
}

class MMPopViewController: EPViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
    }
    
    var delegate : MMPopViewControllerDelegate?
    var popover : UIPopoverPresentationController?
    var array = [String]()
    override func initUI() {
        super.initUI()
        self.view.layer.backgroundColor = "#F6F7F8".color().cgColor
        self.view.layer.masksToBounds = false
        self.view.layer.shadowColor = "#9B9B9B".color().cgColor
        self.view.layer.shadowOffset = CGSize(width:0, height:0)
        self.view.layer.shadowRadius = 4
        self.view.layer.shadowOpacity = 1
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.mj_header = nil
        self.tableView.mj_footer = nil
        self.tableView.bounces = false
        self.tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top:13, left:0, bottom:0, right:0))
        }
    }
    
}

//MARK: - UITableViewDataSource
extension MMPopViewController : UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        var cell = tableView.dequeueReusableCell(withIdentifier: "popcell")
        if cell == nil
        {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: "popcell")
        }
        let text = array[indexPath.row]
        cell!.contentView.removeAllSubView()
        
        let label = UILabel()
        label.text = LS(text)
        label.font = font(14)
        label.textColor = "#005BBA".color()
        label.textAlignment = .center
        cell!.contentView.addSubview(label)
        
        label.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let line = UIView()
        line.backgroundColor = "#EEEEEE".color()
        cell?.contentView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(16)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        cell?.bgHexColor = "#F6F7F8"
        cell?.selectionStyle = .none
        return cell!
    }
}
//MARK: - UITableViewDelegate
extension MMPopViewController : UITableViewDelegate
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        self.dismiss(animated:true, completion:nil)
        self.delegate?.select(indexPath.row, array[indexPath.row] )
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 50
    }
}
